﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntitiesLayer;
using System.Collections;
using System.Transactions;
using System.Data.SqlClient;
using System.Data;
using DAL.Repositories;
using DAL;

namespace DAL.Repositories
{
    public class MusculoRepository : IRepository<Musculo>
    {

        private List<IEntity> _insertItems;
        private List<IEntity> _updateItems;

        public MusculoRepository()
        {
            _insertItems = new List<IEntity>();
            _updateItems = new List<IEntity>();
        }

        public void Insert(Musculo entity)
        {
            _insertItems.Add(entity);
        }

        public void Update(Musculo entity)
        {
            _updateItems.Add(entity);
        }


        public IEnumerable<Musculo> GetAll()
        {


            List<Musculo> listaMusculos = null;

            SqlCommand cmd = new SqlCommand();
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "pa_listar_Musculos");

            if (ds.Tables[0].Rows.Count > 0)
            {
                listaMusculos = new List<Musculo>();

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    listaMusculos.Add(new Musculo
                    {
                        Id = Convert.ToInt32(dr["id"]),
                        Nombre = dr["nombre"].ToString(),
                        Origen = dr["origen"].ToString(),
                        Irrigacion = dr["irrigacion"].ToString(),
                        Insercion = dr["insercion"].ToString(),
                        Inervacion = dr["inervacion"].ToString(),

                    });
                }
            }

            return listaMusculos;
        }

        public Musculo GetById(int id)
        {
            Musculo objMusculo = null;
            var sqlQuery = "SELECT id,nombre,origen,irrigacion ,insercion,inervacion FROM Musculos WHERE id = @id";
            SqlCommand cmd = new SqlCommand(sqlQuery);
            cmd.Parameters.AddWithValue("@id", id);

            var ds = DBAccess.ExecuteQuery(cmd);

            if (ds.Tables[0].Rows.Count > 0)
            {
                var dr = ds.Tables[0].Rows[0];

                objMusculo = new Musculo
                {
                    Id = Convert.ToInt32(dr["id"]),
                    Nombre = dr["nombre"].ToString(),
                    Origen = dr["origen"].ToString(),
                    Irrigacion = dr["irrigacion"].ToString(),
                    Insercion = dr["insercion"].ToString(),
                    Inervacion = dr["inervacion"].ToString(),

                };
            }
            return objMusculo;
        }

        public void Save()
        {
            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    if (_insertItems.Count > 0)
                    {
                        foreach (Musculo objMusculo in _insertItems)
                        {
                            InsertMusculo(objMusculo);
                        }
                    }

                    if (_updateItems.Count > 0)
                    {
                        foreach (Musculo p in _updateItems)
                        {
                            UpdateMusculo(p);
                        }
                    }


                    scope.Complete();
                }
                catch (TransactionAbortedException ex)
                {

                }
                catch (ApplicationException ex)
                {

                }
                finally
                {
                    Clear();
                }

            }
        }

        public void Clear()
        {
            _insertItems.Clear();
            _updateItems.Clear();
        }

        private void InsertMusculo(Musculo objMusculo)
        {
        }

        private void UpdateMusculo(Musculo objMusculo)
        {
        }

    }
}
